//
//  SearchDateView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/19.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "SearchDateView.h"
#import "UIButton+BaseAttributes.h"
#import "UILabel+BasicAttributes.h"
#import "GetDateModel.h"

@interface SearchDateView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIView *dimView;
@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIPickerView *pickView;
@property (nonatomic,strong)NSArray *yearArr;
@property (nonatomic,strong)NSArray *mounthArr;
@property (nonatomic,strong)NSArray *dayArr;

@property (nonatomic,copy)NSString *year;
@property (nonatomic,copy)NSString *mounth;
@property (nonatomic,copy)NSString *day;
@property (nonatomic,copy)NSString *dataStr;
@end

@implementation SearchDateView
- (UIPickerView *)pickView
{
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] init];
        _pickView.dataSource = self;
        _pickView.delegate = self;
    }
    return _pickView;
}
//- (UIView *)lineView
//{
//    if (!_lineView) {
//        _lineView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth*0.9, kScreenHeight, kScreenWidth*0.1, kScreenHeight)];
//        _lineView.backgroundColor = [UIColor whiteColor];
//        [self addSubview:_lineView];
//    }
//    return _lineView;
//}



- (void)setEndDate:(NSDate *)endDate
{
    _endDate = endDate;
    _yearArr = [GetDateModel getYearByCurrentDate:self.endDate];
    
    _mounthArr = [GetDateModel getMounthByCurrentDate:self.endDate];
    
    _dayArr = [GetDateModel getDayByCurrentDate:self.endDate];
    if (_endDate) {
        _year = [_yearArr lastObject];
        _mounth = [_mounthArr lastObject];
        _day = [_dayArr lastObject];
        [self.pickView reloadAllComponents];
        [self.pickView selectRow:self.yearArr.count-1 inComponent:0 animated:YES];
        [self.pickView selectRow:self.mounthArr.count-1 inComponent:1 animated:YES];
        [self.pickView selectRow:self.dayArr.count-1 inComponent:2 animated:YES];
        _dataStr = [NSString stringWithFormat:@"%ld-%.2ld-%.2ld",(long)[_year integerValue],(long)[_mounth integerValue],(long)[_day integerValue]];
        if (_resultDate) {
            _resultDate(_dataStr);
        }
    }
}


//- (NSArray *)yearArr
//{
//    if (!_yearArr) {
//        _yearArr = [GetDateModel getYearByCurrentDate:self.endDate];
//        _year = [_yearArr lastObject];
//    }
//    return _yearArr;
//}
//- (NSArray *)mounthArr
//{
//    if (!_mounthArr) {
//        _mounthArr = [GetDateModel getMounthByCurrentDate:self.endDate];
//        _mounth = [_mounthArr lastObject];
//    }
//    return _mounthArr;
//}
//- (NSArray *)dayArr
//{
//    if (!_dayArr) {
//        _dayArr = [GetDateModel getDayByCurrentDate:self.endDate];
//        _day = [_dayArr lastObject];
//    }
//    return _dayArr;
//}
- (UIView *)dimView
{
    if(!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.alpha = 0.0f;
        _dimView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_dimView];
    }
    return _dimView;
}
- (UIView *)baseView
{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4)];
        _baseView.alpha = 1.0f;
        _baseView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_baseView];
    }
    return _baseView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpUI];
    }
    return self;
}



- (void)setUpUI
{
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
//        self.lineView.frame = CGRectMake(kScreenWidth*0.9, kScreenHeight - kScreenHeight*0.2, kScreenWidth*0.1, kScreenHeight);
        self.dimView.alpha = 0.20f;
    }];
    
    
    
    UIButton *cancelBtn = [UIButton bttonWithTitle:@"取消" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [cancelBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    UIButton *confirmBtn = [UIButton bttonWithTitle:@"完成" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [confirmBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    _titleLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:center];
    _titleLabel.text = self.title;
    [_baseView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(cancelBtn.mas_right);
        make.right.mas_equalTo(confirmBtn.mas_left);
        make.centerY.mas_equalTo(confirmBtn);
    }];
    
    [_baseView addSubview:self.pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_baseView);
        make.top.mas_equalTo(cancelBtn.mas_bottom);
    }];
  }

- (void)layoutSubviews
{
    [super layoutSubviews];
     _titleLabel.text = _title;
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self selfRemoveToSub];
}
- (void)show{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
        //        self.lineView.frame = CGRectMake(kScreenWidth*0.9, kScreenHeight - kScreenHeight*0.2, kScreenWidth*0.1, kScreenHeight);
        self.dimView.alpha = 0.20f;
    }];
}
- (void)selfRemoveToSub
{
    if (_viewHadRemoveFromeSup) {
        _viewHadRemoveFromeSup();
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.dimView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
- (void)buttonDidClick:(UIButton *)sender
{
    [self selfRemoveToSub];
    if ([sender.titleLabel.text isEqualToString:@"取消"]) {
        if (_resultDate) {
            _resultDate(_dataStr);
        }
    }else{
        NSString *dateStr = [NSString stringWithFormat:@"%ld-%.2ld-%.2ld",(long)[_year integerValue],(long)[_mounth integerValue],(long)[_day integerValue]];
        if (_resultDate) {
            _resultDate(dateStr);
        }
    }
    
   
}

#pragma mark - UIPickView 代理 数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.yearArr.count;
    }else if (component == 1){
        return self.mounthArr.count;
    }else{
        return self.dayArr.count;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [NSString stringWithFormat:@"%@年",self.yearArr[row]];
            break;
        case 1:
            return [NSString stringWithFormat:@"%@月",self.mounthArr[row]];
            break;
        case 2:
            return [NSString stringWithFormat:@"%@日",self.dayArr[row]];
            break;
        default:
            return 0;
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            _year = _yearArr[row];
            _mounthArr = [GetDateModel getNormalMouthWithYear:_year andCurrentDate:self.endDate];
            _dayArr = [GetDateModel getNormalDayWithMounth:_mounth andYear:_year andCurrentDate:self.endDate];
            _mounth = [_mounthArr lastObject];
            _day = [_dayArr lastObject];
            [self.pickView reloadComponent:1];
            [self.pickView selectRow:_mounthArr.count-1 inComponent:1 animated:YES];
            [self.pickView reloadComponent:2];
            [self.pickView selectRow:_dayArr.count-1 inComponent:2 animated:YES];
            break;
        case 1:
        {
            _mounth = _mounthArr[row];
            _dayArr = [GetDateModel getNormalDayWithMounth:_mounth andYear:_year andCurrentDate:self.endDate];
            [self.pickView reloadComponent:2];
            _day = [_dayArr lastObject];
            [self.pickView selectRow:_dayArr.count-1 inComponent:2 animated:YES];
        }
            break;
        case 2:
        {
            _day = _dayArr[row];
        }
            break;
        default:
            break;
    }
//    NSString *dateStr = [NSString stringWithFormat:@"%ld-%.2ld-%.2ld",(long)[_year integerValue],(long)[_mounth integerValue],(long)[_day integerValue]];
//
//    if (_resultDate) {
//        _resultDate(dateStr);
//    }
}



@end
